package it.hive.model.io.net.parser.implementations;

import it.hive.mediaplayer.beans.impl.Traccia;
import it.hive.model.Application;
import it.hive.model.domain.media.Gruppo;
import it.hive.model.domain.media.Trasmissione;
import it.hive.model.io.net.parser.interfaces.Parser;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javafx.scene.image.Image;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class M2oXspfParser implements Parser {
	private List<Traccia> trackList;

	@Deprecated
	public M2oXspfParser(InputStream in) throws SAXException, IOException, ParserConfigurationException {
		parse(in);
	}

	@Deprecated
	public M2oXspfParser(File file) throws SAXException, IOException, ParserConfigurationException {
		parse(new FileInputStream(file));
	}

	@Deprecated
	public void parse(InputStream in) throws SAXException, IOException, ParserConfigurationException {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		Document doc = db.parse(in);

		NodeList nl = doc.getElementsByTagName("track");
		trackList = new ArrayList<>();
		for (int i = 0; i < nl.getLength() && i < 30; i++) {

			NodeList locationNlc = doc.getElementsByTagName("location");
			Element locationElements = (Element) locationNlc.item(i);
			URL locationTagValue = new URL(locationElements.getChildNodes().item(0).getNodeValue());

			NodeList imageNlc = doc.getElementsByTagName("image");
			Element imageElements = (Element) imageNlc.item(i);
			Image imageTagValue = new Image(imageElements.getChildNodes().item(0).getNodeValue());

			NodeList annotationNlc = doc.getElementsByTagName("annotation");
			Element annotationElements = (Element) annotationNlc.item(i);
			String annotationTagValue = annotationElements.getChildNodes().item(0).getNodeValue();

			trackList.add(new Traccia(locationTagValue, imageTagValue, annotationTagValue));

			// System.out.println("location :" + locationTagValue);
			// System.out.println("Image :" + imageTagValue);
			// System.out.println("Annotation :" + annotationTagValue + "\n");
		}

	}

	@Override
	public List<Traccia> getTrackList(URL url) {
		return trackList;
	}

	@Override
	public void parse(URL url) {
		try {
			parse(url.openStream());
		} catch (SAXException | IOException | ParserConfigurationException e) {
			Logger.getLogger(Application.class.getName()).log(Level.SEVERE, null, e);
		}

	}

	@Override
	public List<Gruppo> getGruppi(URL url) {
		return null;
	}

	@Override
	public List<Trasmissione> getTrasmissioni(URL url) {
		return null;
	}

}
